<?php
namespace app\index\controller;

use think\Db;
use app\index\model\User as UserModel;
use app\index\validate\User as UserValidate;
use app\index\controller\Base;
class User extends Base
{
    public function index()
    {
        $result = Db::name('courseuser')->alias('a')->join('course b','a.course_id=b.course_id','RIGHT')->where('a.user_id',session('user_id'))->select();
        $this->assign('result',$result);
        return $this->fetch();
    }
    public function add()
    {
        return $this->fetch('user/add');
    }
    public function addCourse()
    {
        $res= Db::name('course')->select();
        $this->assign('data',$res);
        return $this->fetch('user/addCourse');
    }
    public function course()
    {
        $res= Db::name('course')->select();
        $this->assign('data',$res);


        $result = Db::name('courseuser')->alias('a')->join('course b','a.course_id=b.course_id','RIGHT')->where('a.user_id',session('user_id'))->select();
        $this->assign('result',$result);
        return $this->fetch('user/course');
    }
    public function addClass()
    {
        $result = Db::name('class')->select();
        $this->assign('result',$result);
        return $this->fetch('user/addClass');
    }

    public function insert()
    {
        $data = input('post.');
        $val = new UserValidate();
        if (!$val->check($data)) {
            $this->error($val->getError());
            exit();
        }
        $chk = Db::name('person')->where('name',$data['name'])->find();
        if ($chk>0){
            $this->error('用户名已被注册');
        }

        $user = new UserModel($data);
        $ret = $user->allowField(true)->save();
        if ($ret) {
            $this->success('新增用户成功', '/index/User/add');
        } else {
            $this->error('新增用户失败');
        }
    }
}